package com.martin.fast_frame_compose.router

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.martin.fast_frame_compose.base.BaseViewModel
import com.martin.fast_frame_compose.page.dialog.DialogPage
import com.martin.fast_frame_compose.page.home.HomePage
import com.martin.fast_frame_compose.page.main.MainViewModel
import com.martin.fast_frame_compose.page.refresh.RefreshPage
import com.martin.fast_frame_compose.page.test.TestPage
import com.martin.fast_frame_compose.page.user.login.LoginPage

@Composable
fun RouterPage(
    modifier: Modifier = Modifier,
    mainViewModel: MainViewModel,
) {
    mainViewModel.navController = rememberNavController()
    NavHost(
        navController = mainViewModel.navController,
        startDestination = PageRouter.HOME,
        modifier = modifier
    ) {
        composable(PageRouter.TEST) {
            TestPage(vModel = createViewModel(PageRouter.TEST))
        }
        composable(PageRouter.HOME) {
            HomePage(vModel = createViewModel(PageRouter.HOME))
        }
        composable(PageRouter.LOGIN) {
            LoginPage(vModel = createViewModel(PageRouter.LOGIN))
        }
        composable(PageRouter.REFRESH) {
            RefreshPage(vModel = createViewModel(PageRouter.REFRESH))
        }
        composable(PageRouter.DIALOG) {
            DialogPage(vModel = createViewModel(PageRouter.DIALOG))
        }
    }
}

@Composable
private inline fun <reified VM : BaseViewModel> createViewModel(router: String): VM {
    val vModel = viewModel<VM>()
    vModel.router = router
    return vModel
}